//
//  ClassroomchildCell.swift
//  SchoolDemo
//
//  Created by rimi on 16/10/16.
//  Copyright © 2016年 YQ. All rights reserved.
//

import UIKit

class ClassroomchildCell: UITableViewCell {

    // MARK:- 故事版控件属性
    //题目
    @IBOutlet fileprivate weak var ht_titleLabel: UILabel!
    //底部的分割线 用于自适应cell高度
    
    @IBOutlet fileprivate weak var bottomLine: UIView!
    //题目的类型的背景视图
    @IBOutlet fileprivate weak var styleBgView: UIView!
    //题目类型label
    @IBOutlet weak var styleLabel: UILabel!
    
    
    // MARK:- UI控件属性
    //保存选项按钮的数组
    fileprivate var optionButtons:[ClassBtn] = []
    //保存选项内容的label
    fileprivate var optionLabels:[UILabel] = []
    // MARK:-  数据模型属性
    var model:ClasschildModal?{
        didSet{
            guard let model = model,let ht_type = model.ht_type else {
                
                return
            }
            layoutUI(ht_type: ht_type)
            // MARK:- 赋值数据
            ///赋值问题内容
            ht_titleLabel.text = "\((model.ht_order) ?? 0)" + ".  " + (model.ht_title ?? "")
            
            ///赋值选项内容
            let arr = model.ht_option
            for (index,label) in optionLabels.enumerated(){
                let dic = arr?[index] as! NSDictionary
                label.text = dic["ho_text"] as? String
                ///赋值题目类型以及背景视图
                switch model.ht_type?.intValue ?? 0{
                case 1:
                    //单选
                    styleBgView.backgroundColor = UIColor(colorLiteralRed: 254/255.0, green: 103/255.0, blue: 103/255.0, alpha: 1)
                    styleLabel.text = "单选"
                case 2:
                    //多选
                    
                    styleBgView.backgroundColor = UIColor(colorLiteralRed: 50/255.0, green: 191/255.0, blue: 255/255.0, alpha: 1)
                    styleLabel.text = "多选"
                case 3:
                    //判断
                    styleBgView.backgroundColor = UIColor.yellow
                    styleBgView.backgroundColor = UIColor(colorLiteralRed: 252/255.0, green: 187/255.0, blue: 52/255.0, alpha: 1)
                    styleLabel.text = "判断"
                default:
                    break
                }
                
                
                
            }
            ///赋值选择题答案
            guard var answer = model.hs_answer else {
                return
            }
            //            answer = "AB" 模拟的假数据
            for ch in answer.characters{
                switch ch {
                case "A":
                    optionButtons[0].isSelected = true
                case "B":
                    optionButtons[1].isSelected = true
                case "C":
                    optionButtons[2].isSelected = true
                default:
                    optionButtons[3].isSelected = true
                }
                
            }
            
            
        }
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        
    }
    
}
// MARK:- 布局UI
extension  ClassroomchildCell{
    fileprivate func layoutUI(ht_type:NSNumber){
        // MARK:- 设置自身属性
        selectionStyle = .none
        
        // MARK:- 先移除掉原来添加的模型
        optionButtons.removeAll(keepingCapacity: true)
        optionLabels.removeAll(keepingCapacity: true)
        for i in 1...4{
            
            switch ht_type as Int{
            case 1: //单选
                let btn = ClassBtn(btnType: .radio)
                optionButtons.append(btn)
                contentView.addSubview(btn)
                let label  = UILabel()
                optionLabels.append(label)
                contentView.addSubview(label)
                
                break
            case 2: //多选
                let btn  = ClassBtn(btnType: .multiselect)
                optionButtons.append(btn)
                contentView.addSubview(btn)
                let label  = UILabel()
                optionLabels.append(label)
                contentView.addSubview(label)
                
                break
            case 3: //对错
                let btn = ClassBtn(btnType: .judge)
                optionButtons.append(btn)
                contentView.addSubview(btn)
                let label  = UILabel()
                optionLabels.append(label)
                contentView.addSubview(label)
                
                if i == 3{
                    return
                }
            default:
                break
            }
            ///设置按钮的约束
            for (index,value) in optionButtons.enumerated(){
                
                value.snp.makeConstraints({ (make) in
                    make.left.equalTo(35)
                    make.width.height.equalTo(16)
                    make.top.equalTo(ht_titleLabel.snp.bottom).offset(20+24.0 * (CGFloat(index)))
                    
                })
                
                
                
            }
            ///设置label约束以及属性
            for (index,value) in optionLabels.enumerated(){
                value.text = "A.四川常菜"
                value.textAlignment = .left
                value.font = UIFont.systemFont(ofSize: 15)
                
                value.snp.makeConstraints({ (make) in
                    let btn = optionButtons[index]
                    make.left.equalTo(btn.snp.right).offset(8)
                    make.top.equalTo(btn)
                    make.bottom.equalTo(btn)
                })
            }
            
        }
        
        bottomLine.snp.makeConstraints({ (make) in
            make.top.equalTo((optionButtons.last?.snp.bottom)!).offset(20)
        })
        
    }
    
}
